<?php
//header("Content-type: text/ xml");
/**
 * @file
 * User has successfully authenticated with Twitter. Access tokens saved to session and DB.
 */

/* Load required lib files. */
//session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
  return $connection;
}
//$access_token = $_SESSION['access_token'];

//$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

//$connection = getConnectionWithAccessToken($_GET['Access_token'], $_GET['Access_token_secret']);



	function GetTwitterMedia( $GET )
	{
		$connection = getConnectionWithAccessToken("744201864-agn4IQ8yJ3O6CKa2NhbNca7Ow6UvWAJC9SgqEPZG", "Q5FNaKwH3HtTkHy34lbOQOnuosbD0cNgnRR1ptglo0E");
if ($GET['Reset']=='true')
{
	//session_unset();
	$query = $connection->get("https://api.twitter.com/1/statuses/user_timeline.json?count=".$GET['twitter_count']."");
	//$query_count = $query.count;
	
	$_SESSION['max_id'] = $query[count($query)-1]->id_str;
	$_SESSION['Since_id'] = $query[0]->id_str;
}
else
{
	$query = $connection->get("https://api.twitter.com/1/statuses/user_timeline.json?since_id=".$_SESSION['Since_id']."&count=".$GET['twitter_count']."");
	$count_second = $GET['twitter_count'] - count($query);
	$query_Second = $connection->get("https://api.twitter.com/1/statuses/user_timeline.json?max_id='".$_SESSION['max_id']."'&count=$count_second");
	if (!empty($query))
		$_SESSION['Since_id'] = $query[0]->id_str;
	var_dump($query);
}

//var_dump($query);
//echo "Reset".$_GET['Reset'];
$MediaList = array();
foreach ($query as $object)
{
	array_push ($MediaList, MakeMediaArray($object, $connection));
}

if(!empty($query_Second))
{
	$_SESSION['max_id'] = $query_second[count($query_second)-1]->id_str;
	foreach ($query_Second as $object)
	{
		array_push ($MediaList, MakeMediaArray($object, $connection));
/*		$id_str = $object->id_str;
	
		$query_2 = $connection->get("https://api.twitter.com/1/statuses/show.json?id=$id_str&include_entities=1&include_rts=1");

		$entities = $query_2->entities;
	
		$text = $object->text;
		$screen_name = $object->user->screen_name;
		$user = $object->user->name;
		$created_at = $object->created_at;
		$date = date("Y-m-d",strtotime($created_at));
		
		$Split_text = strstr( $text, "http" );
		$media_url = substr($Split_text,7);
		$Description_text = substr($text,0,strpos($text, 'http'));
		$WebLink = "twitter.com/$screen_name/status/$id_str";
		
		echo '<Media ID="'.$id_str.'" MediaType="" CommentCount="" WebLink="'.$WebLink.'" Source="Twitter" Author="'.$user.'" lastUpdate="'.$date.'" >';
		
		//------------------------Media-text Info------------------------//
		echo '<Text ID="'.$id_str.'" Title="" Description="'.$Description_text.'" />';
		
		//------------------------Media-image Info------------------------//
		if(!empty($entities->media))
		{
			$image_url = substr($query_2->entities->media[0]->media_url,7);
			echo '<Image ID="'.$id_str.'" Type="URL" FileName="'.$image_url.'" Description="" />';
			
		}
		
		//------------------------Media-video Info------------------------//
		if(!empty($entities->urls))
		{
			$video_url = $query_2->entities->urls[0]->display_url;
			echo '<Video ID="'.$id_str.'" Type="URL" FileName="'.$video_url.'" Description="" />';
		}
		
		echo '</Media>';
		*/
	}
}//End of IF
		return $MediaList;
	} // End of Function
	
	function MakeMediaArray ( $object, $connection)
	{
		$Media = array();
		$id_str = $object->id_str;
	
		$query_2 = $connection->get("https://api.twitter.com/1/statuses/show.json?id=$id_str&include_entities=1&include_rts=1");

		$entities = $query_2->entities;
	
		$text = $object->text;
		$screen_name = $object->user->screen_name;
		$user = $object->user->name;
		$created_at = $object->created_at;
	//$date = date("Y-m-d",strtotime($created_at));
	
		$Split_text = strstr( $text, "http" );
		$media_url = substr($Split_text,7);
		$Description_text = substr($text,0,strpos($text, 'http'));
		$WebLink = "twitter.com/$screen_name/status/$id_str";
	
	//echo '<Media ID="'.$id_str.'" MediaType="" CommentCount="" WebLink="'.$WebLink.'" Source="Twitter" Author="'.$user.'" lastUpdate="'.$date.'" >';
		$MediaDetial = array( "Head"=>"Media", "ID"=>$id_str, "MediaType"=>"", "CommentCount"=>"", "WebLink"=>$WebLink, "Source"=>"Twitter", "Author"=>$user,"lastUpdate"=>$created_at);
		array_push( $Media, $MediaDetial );
		//------------------------Media-text Info------------------------//
		//echo '<Text ID="'.$id_str.'" Title="" Description="'.$Description_text.'" />';
		$MediaDetial = array( "Head"=>"Text", "ID"=>$id_str, "Title"=>"", "Description"=>$Description_text);
		array_push( $Media, $MediaDetial );
		//------------------------Media-image Info------------------------//
		if(!empty($entities->media))
		{
			$image_url = substr($query_2->entities->media[0]->media_url,7);
			//echo '<Image ID="'.$id_str.'" Type="URL" FileName="'.$image_url.'" Description="" />';
			$MediaDetial = array( "Head"=>"Image", "ID"=>$id_str, "Type"=>'URL', "FileName"=>$image_url, "Description"=>"" );
			array_push( $Media, $MediaDetial );
		}
		
		//------------------------Media-video Info------------------------//
		if(!empty($entities->urls))
		{
			$video_url = $query_2->entities->urls[0]->display_url;
			//echo '<Video ID="'.$id_str.'" Type="URL" FileName="'.$video_url.'" Description="" />';
			$MediaDetial = array( "Head"=>"Video", "ID"=>$id_str, "Type"=>'URL', "FileName"=>$video_url, "Description"=>"");
			array_push( $Media, $MediaDetial );		
		}
		return $Media;
	//echo '</Media>';		
	}
?>